/**
 * Project 1
 *
 * brief description of the program
 *
 * This program is released to public domain according to
 * author's wish, you can view full license at
 * http://tomatoeskit.org/upload/PublicDomainLicense.txt
 *
 * @author Fangning CHENG (Alpha.L Cheng) <chengf@purdue.edu>
 * @lab section number 
 * @date Sep 9, 2013
 *
 */
import java.util.Scanner;

/**
 * Each team has a name and location, represented as String objects, plus offense and
 * defense capabilities represented as double values, randomly initialized between 0 and 1
 * using the luck() method.
 */
public class Team {

    /**
     * The name of the team.
     */
    public String name;

    /**
     * The location of the team.
     */
    public String location;

    /**
     * The offensive strength of the team.
     */
    public double offense;

    /**
     * The defensive strength of the team.
     */
    public double defense;
    
    /**
     * Create a team with specified name and location, and with offense and defense capabilities
     * randomly initialized using the luck() method.
     *
     * @param name
     * @param location
     */
    public Team(String name, String location) {
        offense = luck();
        defense = luck();
        this.name = name;
        this.location = location;
    }     
    
    /**
     * The luck() method returns a random value between 0 and 1, using Math.random().
     * 
     * @returns a real value in range [0,1]
     */
    public double luck() {
        return Math.random();
    }
    
    /**
     * Run a competition against the specified visiting team
     * 
     * @param other the team to play against
     * @returns the winner team
     */
    Team play(Team visitor) {
        //insert your code here
        double home = (this.offense + this.defense + 0.2) * this.luck();
        double away = (visitor.offense + visitor.defense) * visitor.luck();
        if (home > away)
            return this;
        else
            return visitor;
    }
    
    /**
     * Run a competition between two teams specified on standard input.
     * Print statistics of the winner.
     * <p>
     * Each team is read in the following format :
     * <pre>
     * &lt;name&gt; 
     * &lt;location&gt;
     * </pre>
     * (Note that name and location should be separate lines for each team)
     * 
     * @param args can be ignored.
     */
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        // Be sure to follow the same printing command to ask user
        // for name and location of two teams (e.g, home and away)
        System.out.println("Enter name and location for home team (on separate lines)");
        // Get input from user for home team.
        // Create a home team object with the given data.
        Team home = new Team(scanner.nextLine(), scanner.nextLine());
        System.out.println("Enter name and location for away team (on separate lines)");
        // Get input from user for away team.
        // Create an away team object with the given data.
        Team away = new Team(scanner.nextLine(), scanner.nextLine());
        // Print out home team information.
        System.out.printf("Home team is: %s from %s rated %.2f (offense) + %.2f (defense)\n", 
                           home.name, home.location, home.offense, home.defense);
        // Print out away team information.
        System.out.printf("Away team is: %s from %s rated %.2f (offense) + %.2f (defense)\n",
                           away.name, away.location, away.offense, away.defense);
        // Call the home team's play() method with the away team as visitor parameter.
        Team winner = home.play(away);
        // Print out the winner.
        System.out.printf("Winner is: %s from %s rated %.2f (offense) + %.2f (defense)\n",
                           winner.name, winner.location, winner.offense, winner.defense);
        // (be sure to adhere to the format described below)
        scanner.close();
    }
}
